package com.itheima.mapperTest;

import com.itheima.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class mapperDemo {
    InputStream is = null;
    SqlSession session = null;
    public UserMapper Mapper() {

        UserMapper mapper=null;
        try {
            is = Resources.getResourceAsStream("sql.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            session = factory.openSession();
            mapper = session.getMapper(UserMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return mapper;
    }

    public void close()  {
        session.commit();
        session.close();
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
    /*SqlSessionFactory factory = new SqlSessionFactoryBuilder().build();
    SqlSession session = factory.openSession();
    UserMapper mapper = session.getMapper(UserMapper.class);*/

